//
//  GXMoreOperationController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/2.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXMoreOperationController.h"
#import "GXMoreOperationView.h"
#import "GXChangeGroupNameController.h"
@interface GXMoreOperationController
()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * moreTableView;
@property (nonatomic,strong)GXMoreOperationView * moreView;
@property (nonatomic,strong)UIButton * rightItemBtn;
@property (nonatomic,strong)UIButton * moreBtn;
@property(nonatomic, strong) NSMutableArray *deleteArr;//删除数据的数组
@property (nonatomic,assign) NSInteger index;
@end
static NSString *const moreCell=@"MoreCell";
@implementation GXMoreOperationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更多操作";
    _index = -1;
    self.view.backgroundColor = kGlobalbgColor;
    self.deleteArr = [NSMutableArray array];
    [self creatNavigationRightItem];
    [self creatAllView];
}
- (void)creatNavigationRightItem
{
    UIButton * rightButton = [UIButton setButtonTitleNormal:@"" buttonTitlSelect:@"取消" buttonTintColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:13]];
    self.rightItemBtn = rightButton;
    [rightButton addTarget:self action:@selector(clickRightItemButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}
- (void)clickRightItemButton:(UIButton *)btn
{
    if (btn.selected) {
        [self.deleteArr removeAllObjects];
        self.moreTableView.editing = NO;
        self.moreBtn.selected = NO;
        btn.selected = NO;
    }
}

- (void)creatAllView
{
    [self.view addSubview:self.moreTableView];
    [self.view addSubview:self.moreView];
    WeakSelf(weakSelf);
    self.moreView.moreButtonBlock = ^(UIButton * _Nonnull moreButton) {
        weakSelf.moreBtn = moreButton;
        weakSelf.moreTableView.allowsMultipleSelectionDuringEditing = YES;
        weakSelf.moreTableView.editing = !weakSelf.moreTableView.editing;
        weakSelf.rightItemBtn.selected = moreButton.selected;
        if (moreButton.selected) {
            if (weakSelf.moreTableView.editing) {
                weakSelf.index = -1;
            }
        }else{
            if (weakSelf.deleteArr.count) {
                [weakSelf requestDeleteGroupRequestData];
            }
            
        }
    };
}
- (UITableView *)moreTableView
{
    if (_moreTableView == nil) {
        _moreTableView = [[UITableView alloc] initWithFrame:CGRectMake(5,0,kUIScreenWidth-10,kUIScreenHeight-Height_NavBar-Height_TabBar) style:UITableViewStyleGrouped];
        _moreTableView.backgroundColor = kGlobalbgColor;
        _moreTableView.delegate=self;
        _moreTableView.dataSource=self;
        _moreTableView.sectionHeaderHeight = 20;
        _moreTableView.showsHorizontalScrollIndicator=NO;
        _moreTableView.showsVerticalScrollIndicator=NO;
        _moreTableView.editing =NO;
        _moreTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    }
    return _moreTableView;
}
- (GXMoreOperationView *)moreView
{
    if (_moreView == nil) {
        _moreView =[GXMoreOperationView moreOperationView];
        _moreView.frame = CGRectMake(0, kUIScreenHeight-Height_NavBar-40, kUIScreenWidth, 40);
    }
    return _moreView;
}

- (void)requestDeleteGroupRequestData
{
    GXMenuGroupModel *model = self.deleteArr.firstObject;
    [MenuContentManager deleteMenuGroupWithSdishesId:model.dishesId success:^(id responseObject) {
        if (responseObject) {
            [self.dataArray removeObjectsInArray:self.deleteArr];
            [self.moreTableView reloadData];
            [self.deleteArr removeAllObjects];
            [self backSuperViewController:model];
        }
    } fail:^(id responseObject) {
        
    }];

}
- (void)backSuperViewController:(GXMenuGroupModel *)deleteModel
{
    if (self.deleteBolck) {
        self.deleteBolck(deleteModel);
    }
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:moreCell];
    cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:moreCell];
    GXMenuGroupModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 2;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.deleteArr removeAllObjects];
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0]; UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex]; lastCell.accessoryType = UITableViewCellAccessoryNone;
    lastCell.selected = YES;
    _index = indexPath.row;
    [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
    [self.moreTableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:lastIndex afterDelay:.0];
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
    lastCell.selected = NO;
    _index  = -1;
    [self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

@end
